<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="物料号" prop="materialNo">
        <el-input
          v-model="queryParams.materialNo"
          placeholder="请输入物料号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specification">
        <el-input
          v-model="queryParams.specification"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产线" prop="line">
        <el-select
          v-model="queryParams.line"
          placeholder="产线"
          clearable
          style="width: 240px"
          @change="getStationData(queryParams.line)"
        >
          <el-option
            v-for="item in proline_data"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工位" prop="station">
        <el-select
          v-model="queryParams.station"
          placeholder="工位"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in station_data"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="架位" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入架位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="materialType">
        <el-select
          v-model="queryParams.materialType"
          placeholder="类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in type_data"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态1" prop="status1">
        <el-select
          v-model="queryParams.status1"
          placeholder="状态1"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in status1_data"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态2" prop="status2">
        <el-select
          v-model="queryParams.status2"
          placeholder="状态2"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in status2_data"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <!--      <el-form-item label="图片地址" prop="imgUrl">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.imgUrl"-->
      <!--          placeholder="请输入图片地址"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="图纸地址" prop="drawUrl">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.drawUrl"-->
      <!--          placeholder="请输入图纸地址"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:part:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:part:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:part:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:part:export']"
        >导出
        </el-button>
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['wms:part:import']"
        >全局导入
        </el-button>
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleSapImport"
          v-hasPermi="['wms:part:sap:import']"
        >SAP导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleOutUpdate"
        >备件出库
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleReturnUpdate"-->
      <!--        >备件退库-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleInUpdate"
        >备件入库
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handlePrinter"
        >打印条码
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="partList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="物料号" align="center" width="110" prop="materialNo" />
      <el-table-column label="名称" align="left" prop="name" width="120" />
      <el-table-column label="规格型号" align="left" prop="specification" />
      <el-table-column label="品牌" align="left" prop="brand" width="120" />
      <el-table-column label="当前库存" align="center" prop="stock" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="类型" align="center" prop="materialType" width="55" :show-overflow-tooltip="true"/>
      <el-table-column label="状态1" align="center" prop="status1" width="60" :show-overflow-tooltip="true"/>
      <el-table-column label="状态2" align="center" prop="status2" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="单价" align="center" prop="unitPrice" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="SAP数量" align="center" prop="sapStock" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="安全库存" align="center" prop="safetyStock" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="最大库存" align="center" prop="maximalStock" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="产线" align="left" prop="line" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="工位" align="left" prop="station" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="架位" align="left" prop="location" width="110" :show-overflow-tooltip="true"/>
      <!--      <el-table-column label="图片地址" align="center" prop="imgUrl"/>-->
      <!--      <el-table-column label="图纸地址" align="center" prop="drawUrl"/>-->
      <el-table-column label="备注" align="center" prop="remark" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:part:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:part:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料号" prop="materialNo">
          <el-input v-model="form.materialNo" placeholder="请输入物料号" :disabled="true"
                    v-if="title==='修改清单条目信息'"
          />
          <el-input v-model="form.materialNo" placeholder="请输入物料号" v-if="title==='新增清单条目'"/>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="规格型号" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格型号"/>
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌"/>
        </el-form-item>
        <el-form-item label="当前库存" prop="stock">
          <el-input v-model.number="form.stock" placeholder="请输入当前库存" :disabled="true"
                    v-if="title==='修改清单条目信息'"
          />
          <el-input v-model="form.stock" placeholder="请输入当前库存" v-if="title==='新增清单条目'"/>
        </el-form-item>
        <el-form-item label="类型" prop="materialType">
          <el-input v-model="form.materialType" placeholder="请输入当前类型 V或者空"/>
        </el-form-item>
        <!--        <el-form-item label="物料状态1" prop="status1">-->
        <!--          <el-input v-model="form.status1" :disabled="true"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="物料状态2" prop="status2">-->
        <!--          <el-input v-model="form.status2" :disabled="true"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="单价" prop="unitPrice">
          <el-input v-model.number="form.unitPrice" placeholder="请输入单价" :disabled="true"
                    v-if="title==='修改清单条目信息'"
          />
          <el-input v-model.number="form.unitPrice" placeholder="请输入单价" v-if="title==='新增清单条目'"/>
        </el-form-item>
        <el-form-item label="SAP数量" prop="sapStock">
          <el-input v-model.number="form.sapStock" placeholder="请输入SAP数量" :disabled="true"
                    v-if="title==='修改清单条目信息'"
          />
          <el-input v-model.number="form.sapStock" placeholder="请输入SAP数量" v-if="title==='新增清单条目'"/>
        </el-form-item>
        <el-form-item label="安全库存" prop="safetyStock">
          <el-input v-model.number="form.safetyStock" placeholder="请输入安全库存" :disabled="true"
                    v-if="title==='修改清单条目信息'"
          />
          <el-input v-model.number="form.safetyStock" placeholder="请输入安全库存" v-if="title==='新增清单条目'"/>
        </el-form-item>
        <el-form-item label="最大库存" prop="maximalStock">
          <el-input v-model.number="form.maximalStock" placeholder="请输入最大库存" :disabled="true"
                    v-if="title==='修改清单条目信息'"
          />
          <el-input v-model.number="form.maximalStock" placeholder="请输入最大库存" v-if="title==='新增清单条目'"/>
        </el-form-item>
        <el-form-item label="产线" prop="line">
          <el-input v-model="form.line" placeholder="请输入产线"/>
        </el-form-item>
        <el-form-item label="工位" prop="station">
          <el-input v-model="form.station" placeholder="请输入工位"/>
        </el-form-item>
        <el-form-item label="架位" prop="location">
          <el-input v-model="form.location" placeholder="请输入架位"/>
        </el-form-item>
        <!--        <el-form-item label="图片地址" prop="imgUrl">-->
        <!--          <el-input v-model="form.imgUrl" placeholder="请输入图片地址"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="图纸地址" prop="drawUrl">-->
        <!--          <el-input v-model="form.drawUrl" placeholder="请输入图纸地址"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="是否删除" prop="delFlag">-->
        <!--          <el-input v-model="form.delFlag" placeholder="请输入是否删除  0正常  2删除"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate"
          >下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="upload1.title" :visible.sync="upload1.open" width="400px" append-to-body>
      <el-upload
        ref="upload1"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload1.headers"
        :action="upload1.url + '?updateSupport=' + upload1.updateSupport"
        :disabled="upload1.isUploading"
        :on-progress="handleFileUploadProgress1"
        :on-success="handleFileSuccess1"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importSapTemplate"
          >下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm1">确 定</el-button>
        <el-button @click="upload1.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!--    <svg :width="printer.width" :height="printer.height" :fontSize="printer.fontSize" :displayValue="printer.displayValue" :margin="printer.margin"-->
    <!--         :value="printer.value"-->
    <!--         ref="barcode"-->
    <!--    ></svg>-->
    <el-button type="primary" @click="generateBarCodes">批量生成打印标签</el-button>
    <el-button type="primary" v-print="'#printLabels'">批量打印标签</el-button>
    <div id="printLabels">
      <div style="display: flex;flex-wrap: wrap;">
        <div v-for="(barcode, index) in printerPartList" :key="index">
          <div style="width: 150mm;height: 100mm;position: relative">
            <!--            <div style="background-color:#fff;height: 45mm;width: 100mm;">-->
            <!--            <canvas :ref="'barcodeCanvas' + index"  width="100px" height="20px"></canvas>-->
            <!--            <svg :ref="'barcodeCanvas' + index" width="300px" height="100px" style="display: block"></svg>-->
            <!--            <svg :ref="'barcodeCanvas' + index" height="35mm" width="55mm"></svg>-->
            <!--            <svg :ref="'barcodeCanvas' + index"></svg>-->
            <img :ref="'barcodeCanvas' + index" width="500" height="140"/>
            <!--            </div>-->
            <div
              style="font-family: 'Arial Black';font-size: 34px;font-weight: bold;position: absolute;left: 8px;'"
            >
              <div style="">{{ barcode.materialNo }}</div>
              <div style=""><span>{{ barcode.location }}</span> <span style="margin-left: 2rem">{{
                  barcode.name
                }}</span></div>
              <div style="">{{ barcode.specification }}</div>
              <div style="">{{ barcode.brand }}</div>
              <div>CNY {{ barcode.unitPrice }}</div>
            </div>
          </div>
        </div>
      </div>
      <!--        <div v-for="(barcode,index) in printerPartList" :key="index">-->
      <!--          <div style="background-color:#EBEEF5;width: 19.5rem;height: 12rem">-->
      <!--            <div style="margin-left: 4rem">-->
      <!--              <div style="padding:0.2rem 0">{{ barcode.location }} {{ barcode.name }}</div>-->
      <!--              <div style="padding:0.2rem 0">{{ barcode.specification }}</div>-->
      <!--              <div style="padding:0.2rem 0">{{ barcode.brand }} {{ barcode.unitPrice }}</div>-->
      <!--            </div>-->
      <!--          </div>-->
      <!--        </div>-->
      <!--      </vue-easy-print>-->
    </div>
  </div>
</template>
<script>
import {
  addPart,
  delPart,
  getMaterialType,
  getPart,
  getProductLine,
  getStation,
  getStatus1,
  getStatus2,
  listPart, printByIds, printMaterials,
  updatePart
} from '@/api/wms/part'
import { getToken } from '@/utils/auth'
import router from '@/router'
import JsBarcode from 'jsbarcode'
import { treeselect } from '@/api/system/menu'

export default {
  name: 'Part',
  data() {
    return {
      printLoading: true,
      printObj: {
        id: 'printLabels',
        beforeOpenCallback(vue) {
          vue.printLoading = true
          console.log('打开之前')
        },
        openCallback(vue) {
          vue.printLoading = false
          console.log('执行了打印')
        },
        closeCallback(vue) {
          console.log('关闭了打印工具')
        }
      },
      printerPartList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      partList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      add: 0,
      proline_data: [],
      station_data: [],
      type_data: [],
      status1_data: [],
      status2_data: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialNo: null,
        name: null,
        specification: null,
        brand: null,
        stock: null,
        materialType: null,
        status1: null,
        status2: null,
        unitPrice: null,
        sapStock: null,
        safetyStock: null,
        maximalStock: null,
        line: null,
        station: null,
        location: null,
        imgUrl: null,
        drawUrl: null
      },
      form: {},
      // 表单校验
      rules: {
        materialNo: [
          { required: true, message: '物料号不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        specification: [
          { required: true, message: '规格型号不能为空', trigger: 'blur' }
        ],
        brand: [
          { required: true, message: '品牌不能为空', trigger: 'blur' }
        ],
        stock: [
          { required: true, message: '当前库存不能为空', trigger: 'blur' },
          { type: 'number', message: '当前库存必须为数字值', trigger: 'blur' }
        ],
        unitPrice: [
          { type: 'number', message: '单价必须为数字值', trigger: 'blur' }
        ],
        sapStock: [
          { type: 'number', message: 'SAP数量必须为数字值', trigger: 'blur' }
        ], safetyStock: [
          { type: 'number', message: '安全库存必须为数字值', trigger: 'blur' }
        ], maximalStock: [
          { type: 'number', message: '最大库存必须为数字值', trigger: 'blur' }
        ],
        materialType: [],
        line: [
          { required: true, message: '产线不能为空', trigger: 'blur' }
        ],
        station: [
          { required: true, message: '工位不能为空', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '架位不能为空', trigger: 'blur' }
        ]
      },
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/wms/part/importData'
      },
      upload1: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/wms/part/importSapData'
      }

    }
  },
  created() {
    this.getList()
    this.getProductLine()
    this.getMaterialType()
    this.getStatus1()
    this.getStatus2()
  },
  mounted() {

  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    generateBarCodes() {
      // this.generatedBarcodes = [] // 清空已生成的条形码
      this.printerPartList.forEach((barcode, index) => {
        const canvasContainer = this.$refs['barcodeCanvas' + index][0]
        canvasContainer.innerHTML = ''
        // 生成条形码并将其附加到容器
        JsBarcode(canvasContainer, barcode.materialNo, {
          format: 'CODE128',
          lineColor: '#000',
          background: '#fff',
          width: 3,
          height: 120,
          displayValue: false,
          textAlign: 'center',
          textPosition: 'bottom',
          font: 'Arial',
          fontOptions: 'bold'
        })
      })
    },
    getList() {
      this.loading = true
      listPart(this.queryParams).then(response => {
        this.partList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getProductLine() {
      getProductLine().then(response => {
        this.proline_data = response.rows
      })
    },
    getStationData(line) {
      getStation(line).then(
        response => {
          this.station_data = response.rows
        }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        materialNo: null,
        name: null,
        specification: null,
        brand: null,
        stock: null,
        materialType: null,
        status1: null,
        status2: null,
        unitPrice: null,
        sapStock: null,
        safetyStock: null,
        maximalStock: null,
        line: null,
        station: null,
        location: null,
        imgUrl: null,
        drawUrl: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增清单条目'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getPart(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改清单条目信息'
      })
    },
    getMaterialType() {
      getMaterialType().then(response => {
        this.type_data = response.rows
      })
    },
    getStatus1() {
      getStatus1().then(response => {
        this.status1_data = response.rows
      })
    },
    getStatus2() {
      getStatus2().then(response => {
        this.status2_data = response.rows
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePart(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPart(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除此清单条目？').then(function() {
        return delPart(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/part/export', {
        ...this.queryParams
      }, `part_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '全局导入'
      this.upload.open = true
    },
    handleSapImport() {
      this.upload1.title = 'SAP导入'
      this.upload1.open = true
    },
    handleOutUpdate(row) {
      const ids = row.id || this.ids
      const params = { id: ids }
      return router.push({ path: '/warehouse/out', query: params })
    },
    handleInUpdate(row) {
      const ids = row.id || this.ids
      const params = { id: ids }
      return router.push({ path: '/warehouse/in', query: params })
    },
    generateBarcodeCanvas(text) {
      const barcodeCanvas = document.createElement('canvas')
      JsBarcode(barcodeCanvas, text, { format: 'CODE128' })
      return barcodeCanvas
    },
    async handlePrinter(row) {
      const ids = row.id || this.ids
      const resData = await printByIds(ids)
      this.printerPartList = resData.data
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('wms/part/importTemplate', {}, `item_template_${new Date().getTime()}.xlsx`)
    },
    importSapTemplate() {
      this.download('wms/part/importSapTemplate', {}, `item_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert('<div style=\'overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;\'>' + response.msg + '</div>', '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    handleFileUploadProgress1(event, file, fileList) {
      this.upload1.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess1(response, file, fileList) {
      this.upload1.open = false
      this.upload1.isUploading = false
      this.$refs.upload1.clearFiles()
      this.$alert('<div style=\'overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;\'>' + response.msg + '</div>', '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 提交上传文件
    submitFileForm1() {
      this.$refs.upload1.submit()
    }
  }
}
</script>
